<?php
namespace App\Http\Controllers\Weapp\User;

use App\Http\BaseController;
use App\Libraries\Weapp\WeappLib;
use App\Models\Common\MsdMember;
use Illuminate\Http\Request;


class LoginController extends BaseController
{

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    //获取用户的session_key
    public function getSessionKey()
    {
        $code = $this->request->input('code');
        //小程序发送code获取sessionKey
        $result = WeappLib::getSessionKey($code);
        return $this->json_success('数据获取成功！',$result);
    }

    //获取用户的信息
    public function getUserInfo()
    {
        $session_key = $this->request->input('session_key');
        $data = $this->request->input('data');
        $iv = $this->request->input('iv');
        $result = WeappLib::decryptData($session_key, $iv ,$data);
        return $this->json_success('数据获取成功！',$result);
    }

    //小程序用户自动注册
    public function register(Request $request)
    {
        $data = $this->request->all();
        $result = MsdMember::addUser($data);
        $data = login($result->member_id,$request);
        return $this->json_success('注册成功',$data);
    }

    //用户登录
    public function login(Request $request)
    {
        $result = login('1',$request);
        return $this->json_success('数据获取成功',$result);
    }
    
}